
var system = require('system'),
key = system.args[1],
equip = system.args[2],
t = Date.now(),
page = require('webpage').create({
    pageSettings: {   
        loadImages:  false,   
        loadPlugins: true,    
        userAgent: equip?ua(equip):ua()
   },  
    viewportSize: {  
        width: 1024,  
        height: 768  
    }, 
});
phantom.outputEncoding = "gbk"; //指定编码方式
page.open("http://www.baidu.com.cn/s?wd=" + encodeURI(key) + "&cl=3", function (status) {
if (status === "success") {
    var s = page.evaluate(function (key,t,equip) {
        console.log(equip)
        var z = {};
        z.code = 1;
        z.msg = '抓取成功';
        z.word = key;
        z.eq = equip;
        z.time = Date.now() - t;
        z.datalist = [];
        var result = document.querySelectorAll(".c-container");
        Array.prototype.forEach.call(result,function (e) {
            var url =  e.querySelector(".c-img")? e.querySelector(".c-img").src:'';
            if(url){
                var pic = domin(url);
            }
            var data = {
                title: e.querySelector('h3 em')? trim(e.querySelector('h3 em').innerHTML):'',
                info: e.querySelector(".c-row")? trim(e.querySelector(".c-row").textContent):'',
                link: e.querySelector("h3 a")? trim(e.querySelector("h3 a").href):'',
                pic:pic || url,
            };
            z.datalist.push(data)
        })
        function trim(str) {
            return str.replace(/(^\s+)|(\s+$)/g, "");
        }
        function domin(url) {
            var arr = url.split("/"),
                list = {
                "graph.baidu.com": "https://sp0.baidu.com/-aYHfD0a2gU2pMbgoY3K",
                "p.qiao.baidu.com": "https://sp0.baidu.com/5PoXdTebKgQFm2e88IuM_a",
                "vse.baidu.com": "https://sp3.baidu.com/6qUDsjip0QIZ8tyhnq",
                "hdpreload.baidu.com": "https://sp3.baidu.com/7LAWfjuc_wUI8t7jm9iCKT-xh_",
                "lcr.open.baidu.com": "https://sp2.baidu.com/8LUYsjW91Qh3otqbppnN2DJv",
                "kankan.baidu.com": "https://sp3.baidu.com/7bM1dzeaKgQFm2e88IuM_a",
                "xapp.baidu.com": "https://sp2.baidu.com/yLMWfHSm2Q5IlBGlnYG",
                "dr.dh.baidu.com": "https://sp0.baidu.com/-KZ1aD0a2gU2pMbgoY3K",
                "xiaodu.baidu.com": "https://sp0.baidu.com/yLsHczq6KgQFm2e88IuM_a",
                "sensearch.baidu.com": "https://sp1.baidu.com/5b11fzupBgM18t7jm9iCKT-xh_",
                "s1.bdstatic.com": "https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K",
                "olime.baidu.com": "https://sp0.baidu.com/8bg4cTva2gU2pMbgoY3K",
                "app.baidu.com": "https://sp2.baidu.com/9_QWsjip0QIZ8tyhnq",
                "i.baidu.com": "https://sp0.baidu.com/74oIbT3kAMgDnd_",
                "c.baidu.com": "https://sp0.baidu.com/9foIbT3kAMgDnd_",
                "sclick.baidu.com": "https://sp0.baidu.com/5bU_dTmfKgQFm2e88IuM_a",
                "nsclick.baidu.com": "https://sp1.baidu.com/8qUJcD3n0sgCo2Kml5_Y_D3",
                "sestat.baidu.com": "https://sp1.baidu.com/5b1ZeDe5KgQFm2e88IuM_a",
                "eclick.baidu.com": "https://sp3.baidu.com/-0U_dTmfKgQFm2e88IuM_a",
                "api.map.baidu.com": "https://sp2.baidu.com/9_Q4sjOpB1gCo2Kml5_Y_D3",
                "ecma.bdimg.com": "https://ss1.bdstatic.com/-0U0bXSm1A5BphGlnYG",
                "ecmb.bdimg.com": "https://ss0.bdstatic.com/-0U0bnSm1A5BphGlnYG",
                "t1.baidu.com": "https://ss0.baidu.com/6ON1bjeh1BF3odCf",
                "t2.baidu.com": "https://ss1.baidu.com/6OZ1bjeh1BF3odCf",
                "t3.baidu.com": "https://ss2.baidu.com/6OV1bjeh1BF3odCf",
                "t10.baidu.com": "https://ss0.baidu.com/6ONWsjip0QIZ8tyhnq",
                "t11.baidu.com": "https://ss1.baidu.com/6ONXsjip0QIZ8tyhnq",
                "t12.baidu.com": "https://ss2.baidu.com/6ONYsjip0QIZ8tyhnq",
                "i7.baidu.com": "https://ss0.baidu.com/73F1bjeh1BF3odCf",
                "i8.baidu.com": "https://ss0.baidu.com/73x1bjeh1BF3odCf",
                "i9.baidu.com": "https://ss0.baidu.com/73t1bjeh1BF3odCf",
                "b1.bdstatic.com": "https://ss0.bdstatic.com/9uN1bjq8AAUYm2zgoY3K",
                "ss.bdimg.com": "https://ss1.bdstatic.com/5aV1bjqh_Q23odCf",
                "opendata.baidu.com": "https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv",
                "api.open.baidu.com": "https://sp0.baidu.com/9_Q4sjW91Qh3otqbppnN2DJv",
                "tag.baidu.com": "https://sp1.baidu.com/6LMFsjip0QIZ8tyhnq",
                "f3.baidu.com": "https://sp2.baidu.com/-uV1bjeh1BF3odCf",
                "s.share.baidu.com": "https://sp0.baidu.com/5foZdDe71MgCo2Kml5_Y_D3",
                "bdimg.share.baidu.com": "https://ss1.baidu.com/9rA4cT8aBw9FktbgoI7O1ygwehsv",
                "1.su.bdimg.com": "https://ss0.bdstatic.com/k4oZeXSm1A5BphGlnYG",
                "2.su.bdimg.com": "https://ss1.bdstatic.com/kvoZeXSm1A5BphGlnYG",
                "3.su.bdimg.com": "https://ss2.bdstatic.com/kfoZeXSm1A5BphGlnYG",
                "4.su.bdimg.com": "https://ss3.bdstatic.com/lPoZeXSm1A5BphGlnYG",
                "5.su.bdimg.com": "https://ss0.bdstatic.com/l4oZeXSm1A5BphGlnYG",
                "6.su.bdimg.com": "https://ss1.bdstatic.com/lvoZeXSm1A5BphGlnYG",
                "7.su.bdimg.com": "https://ss2.bdstatic.com/lfoZeXSm1A5BphGlnYG",
                "8.su.bdimg.com": "https://ss3.bdstatic.com/iPoZeXSm1A5BphGlnYG"
            };
            return (list[arr[2]]+'/'+arr[3]+'/'+arr[4])
        }
        return encodeURI(JSON.stringify(z))
    },key,t,equip)
     console.log(s)
} else {
    console.log("网页加载失败");
}
phantom.exit(0); //退出系统
});

function ua(facility){
    var list = {
        chrome:'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36',
        firefox:'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:57.0) Gecko/20100101 Firefox/57.0 ',
        IE:'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E; InfoPath.3; rv:11.0) like Gecko ',
        iphone7:'Mozilla/5.0 (iPhone; CPU iPhone OS 10_3 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) CriOS/56.0.2924.75 Mobile/14E5239e Safari/602.1',
        iphone8:'Mozilla/5.0 (iPhone; CPU iPhone OS 10_3 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) CriOS/56.0.2924.75 Mobile/14E5239e Safari/602.1',
        iphoneX:'Mozilla/5.0 (iPhone; CPU iPhone OS 10_3 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) CriOS/56.0.2924.75 Mobile/14E5239e Safari/602.1'
    }
    if(facility){
        return list.facility
    }else{
        return list.chrome
    }
}